<?php
declare (strict_types=1);

namespace app\mall\service\address\admin;

use app\common\service\BaseService;
use app\mall\logic\address\admin\Address as AddressLogic;
use eduline\admin\libs\pageform\FormItem;
use eduline\admin\libs\pagelist\ListItem;
use eduline\admin\page\PageList;
use think\annotation\Inject;
use think\Response;

/**
 * 商品管理
 */
class Address extends BaseService
{

    /**
     * @Inject()
     * @var AddressLogic
     */
    protected $addr;

    /**
     * 地址列表
     * Author: 亓官雨树 <lucky.max@foxmail.com>
     * Date: 22/11/22
     *
     * @return Response
     */
    public function addrList(): Response
    {
        $map = [];
        if (input('?get.phone') && $phone = input('get.phone/d')) $map[] = ['address.phone', '=', $phone];
        if (input('?get.consignee') && $consignee = input('get.consignee/s')) $map[] = ['consignee', '=', $consignee];

        $data = $this->addr->addrList($map);
        // 定义字段
        $keyList   = [
            'user_name' => ListItem::make()->title('用户名')->width(50),
            'consignee' => ListItem::make()->title('收货人')->width(50),
            'phone'     => ListItem::make()->title('收货人手机号')->width(50),
            'areatext'  => ListItem::make()->title('省市区')->width(100),
            'address'   => ListItem::make()->title('详细地址')->width(200),
        ];
        $searchKey = [
            'consignee' => FormItem::make()->title(false)->placeholder('收货人')->maxlength(30),
            'phone'     => FormItem::make()->title(false)->placeholder('手机号')->maxlength(30)
        ];
        // 设置表单
        $list = app(PageList::class);
        // 表单字段
        $list->pageKey   = $keyList;
        $list->searchKey = $searchKey;
        $list->datas     = $data;

        return $list->send();
    }

    public function delAddrs($ids): Response
    {
        $this->addr->delAddrs($ids);
        return static::parseToData([], 1, '删除成功');
    }
}